package com.yuan.member.controller;


import cn.model.Member;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.yuan.common.redis.service.RedisRepository;
import com.yuan.common.redis.service.RedisService;


import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private RedisService redisService;



    @Autowired
    private RedisRepository redisRepository;

    @GetMapping("/info")
    public String info() {
       // int a = 1/0;
        return "info";
    }


    @SentinelResource(value = "list",blockHandler = "listHandler")
    @GetMapping("/list")
    public String list() throws InterruptedException {
        Thread.sleep(5000);
        return "success";
    }
    public String listHandler(BlockException ex) {
        return "线程被流控了"+ex.getMessage();
    }
    @SentinelResource(value = "sentinel",blockHandler = "sentinelHandler")
    @GetMapping("/sentinel")
    public String sentinel() {
        return "sentinel";
    }
    public String sentinelHandler(BlockException ex) {
        return "sentinel被流控了"+ex.getMessage();
    }
//DegradeRule FlowRule
    //流控规则
//    @PostConstruct
//    public void initFlowRules() {
//        List<FlowRule> rules = new ArrayList<>();
//        FlowRule rule = new FlowRule();
//        rule.setResource("userQueryList");  // 资源名称，与@SentinelResource中的value一致
//        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);  // 限流阈值类型(QPS/线程数)
//        rule.setCount(2);  // 每秒最多2次请求
//        rules.add(rule);
//        FlowRuleManager.loadRules(rules);
//    }

    //降级规则
//    @PostConstruct
//    public void initFlowRules() {
//
//        // 流控规则
//        List<FlowRule> flowRules = new ArrayList<>();
//        FlowRule flowRule = new FlowRule();
//        flowRule.setResource("userQueryList");
//        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        flowRule.setCount(2);  // QPS阈值
//        flowRules.add(flowRule);
//        FlowRuleManager.loadRules(flowRules);
//
//        List<DegradeRule> rules = new ArrayList<>();
//        DegradeRule rule = new DegradeRule();
//        rule.setResource("userQueryList");  // 资源名称，与@SentinelResource中的value一致
//        rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
//        //熔断最小异常数量
//        rule.setCount(2);  // 每秒最多2次请求
//
//        //熔断最小请求数量
//        rule.setMinRequestAmount(2);
//        //统计在指定时间内触发的异常和请求数量才出现熔断
//        rule.setStatIntervalMs(60000);
//        //熔断持续时间再此时间内容不在异常将恢复
//        rule.setTimeWindow(10);
//        rules.add(rule);
//        DegradeRuleManager.loadRules(rules);
//    }
//    /**
//     * blockHandler 流控降级后处理的方法 必须声明在同一个类中 优先级比fallback 高
//     * 如果不想声明在同一个类中就必须设置 blockHandlerClass=类名.class 方法必须是静态的static
//     * fallback 接口出现异常
//     * fallbackClass 类似
//     * exceptionsToIgnore 排除哪些异常不处理
//     */
//    // 同时配置blockHandler和fallback
//    @SentinelResource(value = "userQueryList",
//            entryType = EntryType.IN,
//            blockHandler = "blockHandler",
//            fallback = "fallbackHandler")
//    @RequestMapping("/list")
//    public String list() {
//        return "sss";
//    }
//    // 处理流控
//    public String blockHandler(BlockException ex) {
//        return "请求被限流";
//    }

//    // 处理熔断和业务异常
//    public String fallbackHandler(Throwable ex) {
//        return "服务熔断或业务异常: " + ex.getMessage();
//    }
//    @SentinelResource(value = "userQuery", fallback = "fallback")
//    @RequestMapping("/list")
//    public String list()  {
//        int a = 1/0;
//       // Thread.sleep(4000);
//        System.out.println("获取接口");
//        //redisRepository.setExpire("ggg","30",300);
//        //redisService.setCacheObject("sss",123);
//       //Member member = tokenService.getLoginUser("6a5d24050332423a818d0dcab788113e");
//       //System.out.println(member);
//        return "success123";
//    }


}
